package cn.lkx.eduservice.service.impl;

import cn.lkx.eduservice.entity.EduSubject;
import cn.lkx.eduservice.entity.excel.SubjectData;
import cn.lkx.eduservice.entity.subject.OneSubject;
import cn.lkx.eduservice.entity.subject.TwoSubject;
import cn.lkx.eduservice.listener.SubjectExcelListener;
import cn.lkx.eduservice.mapper.EduSubjectMapper;
import cn.lkx.eduservice.service.EduSubjectService;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author lkx
 * @since 2022-05-02
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    //excel 读取
    @Override
    public void saveSubject(MultipartFile file,EduSubjectService subjectService) {

        try {
            //1、获取文件输入流
            InputStream inputStream = file.getInputStream();
            // 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
            EasyExcel.read(inputStream, SubjectData.class,new SubjectExcelListener(subjectService)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    //课程分类列表 树形 (数据封装)
    @Override
    public List<OneSubject> getAllOneTwoSubject() {
        //最终要的到的数据列表
        List<OneSubject> finalList = new ArrayList<>();

        //1、查询所有一级 parent_id = 0
        QueryWrapper<EduSubject> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("parent_id","0"); //父id等于0
        List<EduSubject> oneSubjectList = baseMapper.selectList(queryWrapper1);
        //2、查询所有二级 parent_id != 0
        QueryWrapper<EduSubject> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.ne("parent_id","0"); //父id 不等于0
        List<EduSubject> twoSubjectList = baseMapper.selectList(queryWrapper2);

        //3、封装一级
        for(int i=0; i<oneSubjectList.size(); i++){
            EduSubject eduSubject1 = oneSubjectList.get(i); //变量查询出的一级分类
            OneSubject oneSubject = new OneSubject();  //封装进
            /*oneSubject.setId(eduSubject1.getId());
            oneSubject.setTitle(eduSubject1.getTitle());*/
            BeanUtils.copyProperties(eduSubject1,oneSubject); //工具类把前复制到后(两者有对应的属性才进行复制)
            finalList.add(oneSubject); //一级加入到结果集

            //4、填充一级的二级分类 （children）
            //创建封装每一个一级分类的 二级分类 的集合
            List<TwoSubject> twoList = new ArrayList<>();
            for(int j=0; j<twoSubjectList.size(); j++){ //遍历所有二级从中选自己的孩子
                EduSubject eduSubject2 = twoSubjectList.get(j);
                //找到他对应的一级
                if ( eduSubject2.getParentId().equals( eduSubject1.getId() ) ){
                    TwoSubject twoSubject = new TwoSubject(); //封装进
                    BeanUtils.copyProperties(eduSubject2,twoSubject);
                    twoList.add(twoSubject); //一个一个加入到封装集合
                }
            }
            //封装进一级的 children
            oneSubject.setChildren(twoList);
        }

        return finalList;
    }
}
